package com.kangyi.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.kangyi.admin.mapper.*;
import com.kangyi.admin.service.NursingHomeServiced;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.*;
import com.kangyi.entity.*;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Blank
 * @version 1.0
 */
@Service
@Slf4j
public class NursingHomeServiceImpl implements NursingHomeServiced {

    @Autowired
    private NursingHomeMapper nursingHomeMapper;

    @Autowired
    private NursingHomeTagMapper nursingHomeTagMapper;

    @Autowired
    private NursingHomeRequirementMapper nursingHomeRequirementMapper;

    @Autowired
    private NursingHomeServiceMapper nursingHomeServiceMapper;

    @Override
    public NursingHomeOverviewVO  getOverview(Integer id) {
        NursingHomeOverviewVO nursingHomeOverviewVO = nursingHomeMapper.getOverview(id);
        if (nursingHomeOverviewVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return nursingHomeOverviewVO;
    }

    @Override
    public void updateOverview(NursingHomeOverviewDTO nursingHomeOverviewDTO) {

        Integer id = nursingHomeOverviewDTO.getId();

        nursingHomeMapper.updateImage(id,nursingHomeOverviewDTO.getImage());
        //清除所有
        nursingHomeTagMapper.delete(id);
        //将传回的所有数据重新新增
        List<NursingHomeTagDTO> nursingHomeTagDTOList = nursingHomeOverviewDTO.getNursingHomeTagDTOList();
        if (!CollUtil.isEmpty(nursingHomeTagDTOList)) {
            List<NursingHomeTag> nursingHomeTagList = nursingHomeTagDTOList.stream()
                    .map(nursingHomeTagDTO -> NursingHomeTag.builder()
                            .nursingHomeId(id)
                            .tag(nursingHomeTagDTO.getTag())
                            .build())
                    .collect(Collectors.toList());

            // 调用 mapper 的批量插入方法
            nursingHomeTagMapper.insertBatch(nursingHomeTagList);
        }
    }

    @Override
    public NursingHomeBasicInformationVO inquiryBasicInformation(Integer id) {
        NursingHomeBasicInformationVO NursingHomeBasicInformationVO = nursingHomeMapper.inquiryBasicInformation(id);
        if (NursingHomeBasicInformationVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return NursingHomeBasicInformationVO;
    }

    @Override
    public void updateBasicInformation(NursingHomeBasicInformationDTO nursingHomeBasicInformationDTO) {
        NursingHome nursingHome = new NursingHome();
        BeanUtil.copyProperties(nursingHomeBasicInformationDTO, nursingHome);
        nursingHomeMapper.updateBasicInformation(nursingHome);
    }

    @Override
    public List<NursingHomeRequirementVO> inquiryRequirement(Integer id) {

        List<NursingHomeRequirementVO> nursingHomeRequirementVOList = nursingHomeRequirementMapper.inquiryRequirement(id);
        if (nursingHomeRequirementVOList == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return nursingHomeRequirementVOList;
    }

    @Override
    public void updateRequirement(NursingHomeRequirementDTO nursingHomeRequirementDTO) {

        Integer nursingHomeId = nursingHomeRequirementDTO.getNursingHomeId();

        nursingHomeRequirementMapper.deleteByNursingHomeId(nursingHomeId);

        List<String> requirementList = nursingHomeRequirementDTO.getRequirementList();
        if (!CollUtil.isEmpty(requirementList)) {
            List<NursingHomeRequirement> nursingHomeRequirement = requirementList.stream()
                    .map(requirement -> NursingHomeRequirement.builder()
                        .requirement(requirement)
                        .nursingHomeId(nursingHomeId)
                        .build())
                    .collect(Collectors.toList());

            nursingHomeRequirementMapper.insertBatch(nursingHomeRequirement);
        }
    }
    @Override
    public List<NursingHomeServiceVO> inquiryService(Integer id) {
        NursingHome nursingHome = nursingHomeMapper.selectById(id);
        if (nursingHome == null) {
            throw new DataNotExistException(MessageConstant.NURSING_HOME_NOT_EXIST);
        }
        List<NursingHomeServiceVO> nursingHomeServiceVOList = nursingHomeServiceMapper.inquiryService(id);
        if (CollUtil.isEmpty(nursingHomeServiceVOList)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return nursingHomeServiceVOList;
    }

    @Override
    public void updateService(NursingHomeServiceDetailDTO nursingHomeServiceDetailDTO) {
        Integer nursingHomeId = nursingHomeServiceDetailDTO.getNursingHomeId();

        NursingHome nursingHome = nursingHomeMapper.selectById(nursingHomeId);
        if (nursingHome == null) {
            throw new DataNotExistException(MessageConstant.NURSING_HOME_NOT_EXIST);
        }

        nursingHomeServiceMapper.deleteByNursingHomeId(nursingHomeId);

        List<NursingHomeServiceDTO> nursingHomeServiceDTOList = nursingHomeServiceDetailDTO.getNursingHomeServiceDTOList();
        if (!CollUtil.isEmpty(nursingHomeServiceDTOList)) {
            List<NursingHomeService> nursingHomeServiceList = nursingHomeServiceDTOList.stream()
                    .map(nursingHomeServiceDTO -> NursingHomeService
                            .builder()
                            .nursingHomeId(nursingHomeId)
                            .serviceName(nursingHomeServiceDTO.getServiceName())
                            .description(nursingHomeServiceDTO.getDescription())
                            .icon(nursingHomeServiceDTO.getIcon())
                            .build()).collect(Collectors.toList());

            nursingHomeServiceMapper.insertBatch(nursingHomeServiceList);
        }
    }
}
